package com.hjm.project.team.policy.service.impl;

import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.security.ShiroUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.team.policy.mapper.TeamPolicyMapper;
import com.hjm.project.team.policy.domain.TeamPolicy;
import com.hjm.project.team.policy.service.ITeamPolicyService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 团队政策信息Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-04-11
 */
@Service
public class TeamPolicyServiceImpl implements ITeamPolicyService 
{
    @Resource
    private TeamPolicyMapper teamPolicyMapper;

    /**
     * 查询团队政策信息
     * 
     * @param id 团队政策信息ID
     * @return 团队政策信息
     */
    @Override
    public TeamPolicy selectTeamPolicyById(Integer id)
    {
        return teamPolicyMapper.selectTeamPolicyById(id);
    }

    /**
     * 查询团队政策信息列表
     * 
     * @param teamPolicy 团队政策信息
     * @return 团队政策信息
     */
    @Override
    public List<TeamPolicy> selectTeamPolicyList(TeamPolicy teamPolicy)
    {
        return teamPolicyMapper.selectTeamPolicyList(teamPolicy);
    }

    @Override
    public TeamPolicy selectTeamPolicy(TeamPolicy teamPolicy)
    {
        return teamPolicyMapper.selectTeamPolicy(teamPolicy);
    }

    /**
     * 新增团队政策信息
     * 
     * @param teamPolicy 团队政策信息
     * @return 结果
     */
    @Override
    public int insertTeamPolicy(TeamPolicy teamPolicy) {
        //团奖励类型
        String rewardType = teamPolicy.getRewardType();
        //如果是引流类型 那么档位每个档只能存在一个
        if ("1".equals(teamPolicy.getType())) {
            //查询引流类型的当前档位
            TeamPolicy policy = new TeamPolicy();
            policy.setType("1");
            policy.setPolicyStage(teamPolicy.getPolicyStage());
            if(rewardType != null){
                policy.setRewardType(rewardType);
            }
            TeamPolicy teamPolicy1 = teamPolicyMapper.selectTeamPolicy(policy);
            if (teamPolicy1 != null && teamPolicy1.getPolicyStage().equals(teamPolicy.getPolicyStage()) && teamPolicy1.getRewardType().equals(teamPolicy.getRewardType())) {
                throw new BusinessException("当前引流档政策已配置,请重新选择");
            }
        }
        //选择新团或所有，团奖励类型默认为0
        if(rewardType == null){
            teamPolicy.setRewardType("0");
        }
        teamPolicy.setAddMan(ShiroUtils.getSysUser().getLoginName());
        teamPolicy.setCreateDate(DateUtils.getDate());
        return teamPolicyMapper.insertTeamPolicy(teamPolicy);
    }

    /**
     * 修改团队政策信息
     * 
     * @param teamPolicy 团队政策信息
     * @return 结果
     */
    @Override
    public int updateTeamPolicy(TeamPolicy teamPolicy) {
        //团奖励类型
        String rewardType = teamPolicy.getRewardType();
        //如果是引流类型 那么档位每个档只能存在一个
        if ("1".equals(teamPolicy.getType())) {
            //查询引流类型的当前档位
            TeamPolicy policy = new TeamPolicy();
            policy.setType("1");
            policy.setTeamType(teamPolicy.getTeamType());
            if (teamPolicy.getTeamType().equals("1")) {
                policy.setPolicyStage(teamPolicy.getPolicyStage());
            }
            if(rewardType != null){
                policy.setRewardType(rewardType);
            }
            policy.setPolicyId(teamPolicy.getId().toString());
            TeamPolicy teamPolicy1 = teamPolicyMapper.selectTeamPolicy(policy);
            if (teamPolicy1 != null) {
                if (teamPolicy1.getPolicyStage().equals(teamPolicy.getPolicyStage()) && teamPolicy1.getRewardType().equals(teamPolicy.getRewardType())) {
                    throw new BusinessException("选择的引流档已存在,请修改已存在的政策");
                }
            }
        }
        teamPolicy.setAddMan(ShiroUtils.getSysUser().getLoginName());
        teamPolicy.setUpdateDate(DateUtils.getDate());
        return teamPolicyMapper.updateTeamPolicy(teamPolicy);
    }

    /**
     * 删除团队政策信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTeamPolicyByIds(String ids)
    {
        return teamPolicyMapper.deleteTeamPolicyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除团队政策信息信息
     * 
     * @param id 团队政策信息ID
     * @return 结果
     */
    @Override
    public int deleteTeamPolicyById(Integer id)
    {
        return teamPolicyMapper.deleteTeamPolicyById(id);
    }

    @Override
    public String getPolicyCode() {
        return teamPolicyMapper.getPolicyCode();
    }
}
